import { ILoadMapTypeAll } from '@/common';
import { ManageCommonAPI } from '@/pages/type';
import { getBaseUrl } from '@/services/urlconst';
import { getCompanyID, getLoadMapList, getPosIDs, getTenantID, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { getSupplierTableQuery } from '../mconfig';

const baseUrl = getBaseUrl();
// const baseUrl = 'http://192.168.153.246:31019'; //旭潮本地

/**
 * 添加联系人
 * @param callback  调用接口后的回调
 */
const addContacterService = (callback: (res: any) => void) => {
  const addContacter = useRequest(
    (payload: any) => {
      let reqUrl = `${baseUrl}/supplierContacterController/add`;
      let methodType = 'POST';
      let bodyParams: any = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    addContacterRun: addContacter?.run,
    addContacterLoading: addContacter?.loading,
  };
};

/**
 * 编辑联系人
 * @param callback  调用接口后的回调
 */
const editContacterService = (callback: (res: any) => void) => {
  const editContacter = useRequest(
    (payload: any) => {
      let reqUrl = `${baseUrl}/supplierContacterController/edit`;
      let methodType = 'PUT';
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    editContacterRun: editContacter?.run,
    editContacterLoading: editContacter?.loading,
  };
};

/**
 * 获取联系人详情
 * @param callback
 */
const getContacterService = (callback: (res: any) => void) => {
  const getContacter = useRequest(
    (payload: any) => {
      let reqUrl = `${baseUrl}/supplierContacterController/getContacterById/${payload?.id}`;
      let methodType = 'GET';
      return requestFunComponFiex(methodType, reqUrl, {});
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    getContacterRun: getContacter?.run,
    getContacterLoading: getContacter?.loading,
    getContacterInfo: getContacter?.data?.obj,
  };
};

/**
 * 删除联系人
 * @param callback  调用接口后的回调
 */
const deleteContacterService = (callback: (res: any) => void) => {
  const deleteContacter = useRequest(
    (payload: any) => {
      let reqUrl = `${baseUrl}/supplierContacterController/delete/${payload?.id}`;
      let methodType = 'DELETE';
      return requestFunComponFiex(methodType, reqUrl, {});
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    deleteContacterRun: deleteContacter?.run,
    deleteContacterLoading: deleteContacter?.loading,
  };
};

/**
 * 获取供应商
 * @param curRes 权限
 * @returns
 */
const supplierDataGridServices = (curRes: Record<string, any>) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.COMPANY_TYPE}`, //企业类型
    `${getTenantID()}${ILoadMapTypeAll.CLASSIFICATION_SUPPLY}`, //供货类型
    `${getTenantID()}${ILoadMapTypeAll.SUPPLIER_STATE}`, //企业类型
  ];

  /** 获取数据字典loadMdicMap */
  const { loadMapInfo, mapLoading } = getLoadMapList(loadMdicIds);

  /** 获取权限参数 */
  const queryObj: Record<string, any> = getSupplierTableQuery(curRes);

  /** 获取列表数据  */
  const { loading, run, refresh, tableProps, data } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        current: 1,
        pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        dicTypeIds: loadMdicIds?.toString(),
        isBlack: 0,
        ...queryObj,
        ...opt,
      };

      const requestURL = `${baseUrl}/supplierInfoController/dataGrid/${requestBody?.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: false, //页面初始请求为 true
      // manual: true, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /**
   * 获取管理也数据
   * @param otherRequest 搜索参数
   */
  const getSupplierDataGrid = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };
    run({ ...request });
  };

  return {
    getSupplierDataGrid,
    refreshSupplierDataGrid: refresh,
    supplierDataGridloading: loading,
    supplierPagination: tableProps.pagination,
    supplierDataGrid: data?.rows,
    mapLoading,
    loadMapInfo,
  };
};

export { addContacterService, editContacterService, getContacterService, deleteContacterService, supplierDataGridServices };
